package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.enums.*;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.business.model.BaseQueryRequest;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.joda.time.DateTime;

import java.util.Objects;

/**
 * @Author wangxiangdong
 * @Description 在催案件查询request
 * @Date 2019/9/2 15:07
 **/
@Data
public class CollectingCasesRequest extends BaseQueryRequest {

    @ApiModelProperty(notes = "记事本")
    private String comment;

    @ApiModelProperty(value = "是否管理员")
    private BaseSelect isManage;

    @ApiModelProperty(value = "催收员ID")
    private String userId;

    @ApiModelProperty(value = "部门code")
    private String code;

    @ApiModelProperty(value = "催记数量")
    private Integer followRecordCount;

    @ApiModelProperty(value = "催收状态")
    private String collectStatus;

    @ApiModelProperty(value = "联系状态")
    private String contactStatus;

    @ApiModelProperty(value = "催收人员")
    private String collectorName;

    @ApiModelProperty(value = "催收机构code")
    private String departCode;

    @ApiModelProperty(value = "催记数量")
    private Integer telRecordCount;

    @ApiModelProperty(value = "案件分类")
    private String caseFiller;

    @ApiModelProperty(notes = "留案标识")
    private CaseLeaveFlag leaveFlag;

    @ApiModelProperty(value = "是否协催")
    private BaseSelect isAssist;

    @ApiModelProperty(value = "案件类型")
    private CaseType caseType;

    public BooleanBuilder generateQueryBuilder() {
        DateTime dateTime = new DateTime(ZWDateUtil.getNowDate());
        BooleanBuilder booleanBuilder = generateBaseQueryBuilder();
        QCaseInfo caseInfo = QCaseInfo.caseInfo;
        booleanBuilder.and(caseInfo.caseDataStatus.eq(CaseDataStatus.IN_POOL));
        booleanBuilder.and(caseInfo.publicCaseFlag.eq(PublicCaseFlag.NO_PUBLIC_CASE));
        if (Objects.nonNull(this.comment)) {
            booleanBuilder.and(caseInfo.comment.eq(this.comment));
        }
        if (Objects.nonNull(this.isManage)) {
            if (this.isManage.equals(BaseSelect.YES)) {
                booleanBuilder.and(caseInfo.currentCollector.department.departCode.like(this.code + "%"));
            } else {
                booleanBuilder.and(caseInfo.currentCollector.id.eq(userId));
            }
        }
        if (Objects.nonNull(this.followRecordCount)) {
            booleanBuilder.and(caseInfo.telRecordCount.eq(this.followRecordCount));
        }
        if (Objects.nonNull(this.collectStatus)) {
            booleanBuilder.and(caseInfo.collectStatus.contains(this.collectStatus));
        }
        if (Objects.nonNull(this.leaveFlag)) {
            booleanBuilder.and(caseInfo.leaveFlag.eq(this.leaveFlag));
        }
        if (Objects.nonNull(this.collectorName)) {
            booleanBuilder.and(caseInfo.currentCollector.realName.like("%" + this.collectorName + "%"));
        }
        if (Objects.nonNull(this.departCode)) {
            booleanBuilder.and(caseInfo.currentCollector.department.departCode.like(this.departCode + "%"));
        }
        if (Objects.nonNull(this.telRecordCount)) {
            booleanBuilder.and(caseInfo.telRecordCount.eq(this.telRecordCount));
        }
        if (Objects.nonNull(this.contactStatus)) {
            booleanBuilder.and(caseInfo.contactStatus.contains(this.contactStatus));
        }
        if (Objects.nonNull(this.isAssist)) {
            booleanBuilder.and(caseInfo.isAssist.eq(this.isAssist));
        }
        if (Objects.nonNull(this.caseType)) {
            booleanBuilder.and(caseInfo.caseType.eq(this.caseType));
        }
        if (Objects.nonNull(this.caseFiller)) {
            switch (this.caseFiller) {
                case "TODAY_NO_FOLLOW":
                    booleanBuilder.andAnyOf(caseInfo.followTime.isNull(), caseInfo.followTime.lt(ZWDateUtil.getNowDate()));
                    break;
                case "TODAY_FOLLOW":
                    booleanBuilder.and(caseInfo.followTime.goe(ZWDateUtil.getNowDate()));
                    break;
                case "MAJOR_FOLLOW":
                    booleanBuilder.and(caseInfo.isImportant.eq(BaseSelect.YES));
                    break;
                case "ONE_TO_THREE_NO_FOLLOW":
                    booleanBuilder.and(
                            (caseInfo.followTime.goe(dateTime.minusDays(3).toDate()).and(caseInfo.followTime.lt(dateTime.toDate())))
                                    .or(caseInfo.followTime.isNull().and(caseInfo.transferDate.goe(dateTime.minusDays(3).toDate())).and(caseInfo.transferDate.lt(dateTime.toDate())))
                    );
                    break;
                case "FOUR_TO_SIX_NO_FOLLOW":
                    booleanBuilder.and(
                            (caseInfo.followTime.goe(dateTime.minusDays(6).toDate()).and(caseInfo.followTime.lt(dateTime.minusDays(3).toDate())))
                                    .or(caseInfo.followTime.isNull().and(caseInfo.transferDate.goe(dateTime.minusDays(6).toDate())).and(caseInfo.transferDate.lt(dateTime.minusDays(3).toDate())))
                    );
                    break;
                case "PTP":
                    booleanBuilder.and(caseInfo.lastContactResult.eq("承诺还款"));
                    break;
                case "THREE_DAYS_LEFT":
                    booleanBuilder.and(caseInfo.endCaseDate.goe(dateTime.toDate())).and(caseInfo.endCaseDate.loe(dateTime.plusDays(3).toDate()));
                    break;
                case "SEVEN_DAYS_LEFT":
                    booleanBuilder.and(caseInfo.endCaseDate.goe(dateTime.toDate())).and(caseInfo.endCaseDate.loe(dateTime.plusDays(7).toDate()));
                    break;
                default:
                    booleanBuilder.andAnyOf(caseInfo.followTime.isNull(), caseInfo.followTime.lt(ZWDateUtil.getNowDate()));
                    break;
            }
        }
        return booleanBuilder;
    }

}
